<?php
/**
 * Sanpham_Controller
 * - Controller for module page Home
 * @author ANHTAM
 * @since 1.1 - Sep 27, 2011
 * $Id: $
 */
// Require module for controller
class Sanpham_Controller extends ControllerAdmin {
	/**
     * - Constructor
     */
    public function __construct() {
    	parent::__construct();
    	$this->menuDanhMuc = true;
    	$this->errorArr = array();
    }
    
    /**
     * actionIndex
     * - page index of controller
     */
    public function actionIndex() {
    	
    	// Process init
        $objView = new ViewAdmin();
        $this->tpl_title = 'Sản phẩm';
        $this->tpl_title_list = '';
        $this->arrContent = array();
        $this->currPage = '';
		$this->numberPage = '';
		$this->strPaging = '';
        
    	if (!$this->loginFlag || $this->dataUser['quyen'] > '1') {
        	FunctionCommon::sendRedirect();
        }
        
        $dataGet = FunctionCommon::getGet(array('danhmucid', 'iddm', 'subiddm'));
        $this->iddm = !empty($dataGet['iddm']) ? $dataGet['iddm'] : '';
        $this->subiddm = !empty($dataGet['subiddm']) ? $dataGet['subiddm'] : '';
    	$danhmucid = $dataGet['danhmucid'];
        if ($danhmucid != ''){
        	Session::Set("danhmucid", $danhmucid);
        } else {
        	$danhmucid = Session::Get("danhmucid");
        }
        $this->danhmucid = $danhmucid;

    	$ojbSanPham = new MSanpham();
	    if ($this->isPost()) {
	        $arrPost = FunctionCommon::getPost(array('itemID', 'mode', 'type', 'checkdel'));
			// Delete by id
			if ($arrPost['mode'] == 'delete') {
			     $dataSanpham = $ojbSanPham->getByPrimaryKey($arrPost['itemID']);
			     $ojbSanPham->delete(array('id' => $arrPost['itemID']));
			     FunctionCommon::move(HTDOC_IMAGES_DIR."/img-the/".$dataSanpham['code']);
			     FunctionCommon::move(HTDOC_IMAGES_DIR."/img-the-min/".$dataSanpham['code']);
			}
			        
			// Delete all by array id
			if ($arrPost['mode'] == 'deleteall') {
			    if (count ($arrPost['checkdel']) > 0) {
			    	foreach ($arrPost['checkdel'] as $aP) {
			    		$dataSanpham = $ojbSanPham->getByPrimaryKey($aP);
			        	$ojbSanPham->delete(array('id' => $aP));
			        	FunctionCommon::move(HTDOC_IMAGES_DIR."/img-the/".$dataSanpham['code']);
			        	FunctionCommon::move(HTDOC_IMAGES_DIR."/img-the-min/".$dataSanpham['code']);
			    	}
			    }
			}
			
	    	// Up
	        if ($arrPost['mode'] == 'up') {
	        	$id = $arrPost['itemID'];
	        	$seq = $arrPost['type'];
	        	$seqMin = $ojbSanPham->getMinMax(array("seq"=>array("math"=>">", "value"=>$seq), "danhmuc_id"=>array("math"=>"=", "value"=>$danhmucid), "iddm"=>array("math"=>"=", "value"=>$this->iddm), "iddmc"=>array("math"=>"=", "value"=>$this->subiddm)));
	        	$idMin = $ojbSanPham->getIdBySeq($seqMin);
	        	
	        	$ojbSanPham->setAttribute('seq', $seqMin);
	        	$ojbSanPham->update(array('id' => $id));
	        	$ojbSanPham->emptyAttribute();
	        	$ojbSanPham->setAttribute('seq', $seq);
	        	$ojbSanPham->update(array('id' => $idMin));
	        }
	        
	    	// Down
	        if ($arrPost['mode'] == 'down') {
	        	$id = $arrPost['itemID'];
	        	$seq = $arrPost['type'];
	        	$seqMax = $ojbSanPham->getMinMax(array("seq"=>array("math"=>"<", "value"=>$seq), "danhmuc_id"=>array("math"=>"=", "value"=>$danhmucid), "iddm"=>array("math"=>"=", "value"=>$this->iddm), "iddmc"=>array("math"=>"=", "value"=>$this->subiddm)), true);
	        	$idMax = $ojbSanPham->getIdBySeq($seqMax);
	        	
	        	$ojbSanPham->setAttribute('seq', $seqMax);
	        	$ojbSanPham->update(array('id' => $id));
	        	$ojbSanPham->emptyAttribute();
	        	$ojbSanPham->setAttribute('seq', $seq);
	        	$ojbSanPham->update(array('id' => $idMax));
	        }
	   	}
	   	$this->maxseq = $ojbSanPham->getMinMax(array('danhmuc_id'=>array('math'=>'=', 'value'=>$danhmucid), "iddm"=>array("math"=>"=", "value"=>$this->iddm), "iddmc"=>array("math"=>"=", "value"=>$this->subiddm)), true);
        $this->minseq = $ojbSanPham->getMinMax(array('danhmuc_id'=>array('math'=>'=', 'value'=>$danhmucid), "iddm"=>array("math"=>"=", "value"=>$this->iddm), "iddmc"=>array("math"=>"=", "value"=>$this->subiddm)));
        	
	    // List all
		$arrParam = array('m' => 'sanpham', 'iddm'=>$this->iddm, 'subiddm'=>$this->subiddm);
		$arrField = array('id', 'seq', 'iddm', 'iddmc', 'name', 'code', 'gia1', 'check_km', 'gia2', 'check_dis');
		$condition = array("iddm"=>array('value'=>$this->iddm, 'math'=>'='), "iddmc"=>array('value'=>$this->subiddm, 'math'=>'='), "danhmuc_id"=>array("math"=>"=", "value"=>$danhmucid));
				
		$data = FunctionCommon::paging($ojbSanPham, $arrParam, $arrField, $condition);//$this->paging($arrParam, $arrField, $condition);
		$arrContent = $data["data"];
		if (count ($arrContent) > 0) {
			foreach ($arrContent as $content) {
				$this->arrContent[] = $content;
			}
			$this->currPage = $data["currPage"];
			$this->numberPage = $data["numberPage"];
			$this->strPaging = $data["strPaging"];
		}

        // Process view
        $objView->assignobj($this);
        $objView->display();
    }

	/**
     * actionEdit
     * - page edit of controller
     */
    public function actionEdit() {
    	
    	// Process init
        $objView = new ViewAdmin();
        $this->flagSP = true;
        $this->tpl_title = 'Thêm sản phẩm';
        $this->tpl_button = "Thêm mới";
        $this->arrField = array();
        $this->tpl_title_list = 'Manager product -> Product Add/Edit';
        $this->dialog = false;
        $this->id = '';
        $this->tenFile = '';
        
    	if (!$this->loginFlag || Session::Get("danhmucid") == '' || $this->dataUser['quyen'] > '1') {
        	FunctionCommon::sendRedirect();
        }
        $this->danhmucid = Session::Get("danhmucid");
        $dataGet = FunctionCommon::getGet(array('iddm', 'subiddm', 'by', 'dis', 'back', 'idSP'));
        $this->iddm = $dataGet['iddm'];
        $this->subiddm = $dataGet['subiddm'];
        $this->by = $dataGet['by'];
        $this->arrField = FunctionCommon::getPost(array('name', 'mota', 'gia1', 'check_dis', 'check_km', 'gia2', 'txtKM', 'soluong', 'nameimage', 'itemID', 'mode', 'type'));
        $this->dis = $dataGet['dis'];
        $this->back = $dataGet['back'];
        $this->idSP = $dataGet['idSP'];
        
        if ($this->isPost()) {       	
        	$this->id = $this->arrField['itemID'];
        	
        	if ($this->arrField['mode'] != '') {
        		if ($this->arrField['type'] == 'edit') {
	        		if (!Validator::isIntegerPositive($this->arrField['gia1']) || $this->arrField['gia1'] == '') {
		        		$this->errorArr['gia1'] = "Giá sản phẩm này phải là số";
		        	}
		        	if (!Validator::isIntegerPositive($this->arrField['gia2'])) {
		        		$this->errorArr['gia2'] = "Giá khuyến mãi phải là số";
		        	}
		        	if ($this->arrField['check_km'] != '' && $this->arrField['gia2'] == '') {
		        		$this->errorArr['gia2'] = "Bạn hãy nhập phần giá khuyến mãi";
		        	}
		        	
	        		if (count ($this->errorArr) == 0) {
		        		// Update du lieu
		        		if ($this->id != '') {
		        			$ojbSanPham = new MSanpham();
		        			$ojbSanPham->setAttribute('code', $this->arrField['nameimage']);       			
			        		$ojbSanPham->setAttribute('name', $this->arrField['name']);
			        		$ojbSanPham->setAttribute('gia1', ($this->arrField['gia1']!='') ? $this->arrField['gia1'] : 0);
			        		$ojbSanPham->setAttribute('check_dis', ($this->arrField['check_dis']!='') ? $this->arrField['check_dis'] : 0);
				        	$ojbSanPham->setAttribute('check_km', ($this->arrField['check_km']!='') ? $this->arrField['check_km'] : 0);
				        	$ojbSanPham->setAttribute('gia2', ($this->arrField['gia2'] != '') ? $this->arrField['gia2'] : 0);
				        	$ojbSanPham->setAttribute('txtKM', $this->arrField['txtKM']);
				        	$ojbSanPham->setAttribute('soluong', $this->arrField['soluong']);
			        		$ojbSanPham->setAttribute('mota', $this->arrField['mota']);
			        		
				        	$ojbSanPham->update(array('id' => $this->id));
				        	$this->dialog = true;
		        		}
		        		
		        		// Insert du lieu
			        	if ($this->id == '') {
			        		if ($this->subiddm == '') {
			        			$ojbSCate = new SanphamSCategory();
			        			if ($ojbSCate->checkTonTai($this->danhmucid, $this->iddm)) {
				        			$ojbSCate->setAttribute('danhmuc_id', $this->danhmucid);
				        			$ojbSCate->setAttribute('iddm', $this->iddm);
				        			$ojbSCate->setAttribute('seq', $ojbSCate->getMinMax(array(), true) + 1);
				        			$ojbSCate->setAttribute('insert_at', date('Y-m-d H:i:s'));
				        			$this->subiddm = $ojbSCate->insert();
				        			$ojbSCate->emptyAttribute();
			        			} else {
			        				$this->subiddm = $ojbSCate->getIDDMC($this->danhmucid, $this->iddm);
			        			}
			        		}
			        		$ojbSanPham = new MSanpham();
			        		$ojbSanPham->setAttribute('danhmuc_id', $this->danhmucid);
				    		$ojbSanPham->setAttribute('seq', $ojbSanPham->getMinMax(array(), true) + 1);
				    		$ojbSanPham->setAttribute('iddm', $this->iddm);
				    		$ojbSanPham->setAttribute('iddmc', $this->subiddm);
				    		$ojbSanPham->setAttribute('code', $this->arrField['nameimage']);
				    		$ojbSanPham->setAttribute('name', $this->arrField['name']);
				    		$ojbSanPham->setAttribute('gia1', ($this->arrField['gia1']!='') ? $this->arrField['gia1'] : 0);
				    		$ojbSanPham->setAttribute('check_dis', ($this->arrField['check_dis']!='') ? $this->arrField['check_dis'] : 0);
				        	$ojbSanPham->setAttribute('check_km', ($this->arrField['check_km']!='') ? $this->arrField['check_km'] : 0);
				        	$ojbSanPham->setAttribute('gia2', ($this->arrField['gia2'] != '') ? $this->arrField['gia2'] : 0);
				        	$ojbSanPham->setAttribute('txtKM', $this->arrField['txtKM']);
				        	$ojbSanPham->setAttribute('soluong', $this->arrField['soluong']);    
					        $ojbSanPham->setAttribute('mota', $this->arrField['mota']);
				        	$ojbSanPham->setAttribute('insert_at', date('Y-m-d H:i:s'));
				        	$ojbSanPham->insert();
				        	$this->dialog = true;
				        }
	        		}
        		}
        		
        		// Xu ly file
	        	if ($this->arrField['type'] == 'upload') {
	        		$this->tenFile = FunctionCommon::uploadFileTo($_FILES["code"], "img-the", $this->arrField['nameimage']);
	        		if ($this->tenFile == false) {
	        			$this->tenFile = $this->arrField['nameimage'];
	        		}
		        }
		        
		        // Delete image
	        	if ($this->arrField['type'] == 'delete') {
		        	$tenFile = $this->arrField['nameimage'];
		        	FunctionCommon::move(HTDOC_IMAGES_DIR."/img-the/".$tenFile);
		        	FunctionCommon::move(HTDOC_IMAGES_DIR."/img-the-min/".$tenFile);
		        }
		        
        	} else {
        		$this->tpl_title = 'Sửa sản phẩm';
        		$this->tpl_button = "Sửa";
	        	$ojbSanpham = new MSanpham();
	        	$this->arrField = $ojbSanpham->getByPrimaryKey($this->id);
	        	$this->tenFile = $this->arrField["code"];
        	}
        }

        // Process view
        $objView->assignobj($this);
        $objView->display();
    }
    
    /**
     * actionDisplay
     * - page display of controller
     */
    public function actionDisplay() {
    	// Process init
        $objView = new ViewAdmin();
        $this->flagDSP = true;
        $this->tpl_title = 'Hiển thị sản phẩm khuyến mãi';
        $this->tpl_title_list = 'Hiển thị sản phẩm khuyến mãi';
        
    	if (!$this->loginFlag || $this->dataUser['quyen'] > '1') {
        	FunctionCommon::sendRedirect();
        }
        
        $dataGet = FunctionCommon::getGet(array('danhmucid', 'iddm', 'subiddm', 'back', 'by'));
        
        $this->danhmucid = ($dataGet['danhmucid']) != '' ? $dataGet['danhmucid'] : '1';
        Session::Set("danhmucid", $this->danhmucid);
        $this->iddm = !empty($dataGet['iddm']) ? $dataGet['iddm'] : '';
        $this->subiddm = !empty($dataGet['subiddm']) ? $dataGet['subiddm'] : '';
        $this->back = $dataGet['back'];
        $this->by = $dataGet['by'];
        
        $ojbSanPham = new MSanpham();
        if ($this->isPost()) {
        	$dataPost = FunctionCommon::getPost(array('itemID', 'mode', 'type', 'check_dis', 'data'));
        	
        	// Check display
        	if ($dataPost['mode'] == 'no') {
	        	$arrData = $dataPost['data'];
	        	$arrId = $dataPost['check_dis'];
	        	if (count ($arrData) > 0) {
		        	foreach ($arrData as $id) {
		        		if (count ($arrId) > 0 && in_array($id, $arrId)) {
		        			$ojbSanPham->setAttribute('check_dis', 1);
				        	$ojbSanPham->update(array('id' => $id));
		        		} else {
		        			$ojbSanPham->setAttribute('check_dis', 0);
				        	$ojbSanPham->update(array('id' => $id));
		        		}
		        	}
	        	}
        	}
        	if ($dataPost['mode'] == 'yes') {
	        	$arrData = $dataPost['data'];
	        	$arrId = $dataPost['check_dis'];
	        	if (count ($arrData) > 0) {
		        	foreach ($arrData as $id) {
		        		if (count ($arrId) > 0 && in_array($id, $arrId)) {
		        			$ojbSanPham->setAttribute('check_dis', 1);
				        	$ojbSanPham->update(array('id' => $id));
		        		} else {
		        			$ojbSanPham->setAttribute('check_dis', 0);
				        	$ojbSanPham->update(array('id' => $id));
		        		}
		        	}
	        	}
        	}
        	
        	// Up
        	if ($dataPost['mode'] == 'up') {
        		$id = $dataPost['itemID'];
	        	$seq = $dataPost['type'];
	        	if ($this->iddm != '' && $this->subiddm != '') {
	        		$seqMin = $ojbSanPham->getMinMax(array('seq'=>array('math'=>'>', 'value'=>$seq), 
	        											   'check_dis'=> array('math'=>'=', 'value'=>'1'),
	        											   'danhmuc_id'=> array('math'=>'=', 'value'=>$this->danhmucid),
        												   'iddm'=> array('math'=>'=', 'value'=>$this->iddm),
        												   'iddmc'=> array('math'=>'=', 'value'=>$this->subiddm)));
	        	} else {
	        		$seqMin = $ojbSanPham->getMinMax(array('seq'=>array('math'=>'>', 'value'=>$seq), 'check_dis'=> array('math'=>'=', 'value'=>'1')));
	        	}
	        	$idMin = $ojbSanPham->getIdBySeq($seqMin);
	        	
	        	$ojbSanPham->setAttribute('seq', $seqMin);
	        	$ojbSanPham->update(array('id' => $id));
	        	$ojbSanPham->emptyAttribute();
	        	$ojbSanPham->setAttribute('seq', $seq);
	        	$ojbSanPham->update(array('id' => $idMin));
        	}
        	
        	// Down
        	if ($dataPost['mode'] == 'down') {
        		$id = $dataPost['itemID'];
	        	$seq = $dataPost['type'];
	        	if ($this->iddm != '' && $this->subiddm != '') {
	        		$seqMax = $ojbSanPham->getMinMax(array('seq'=>array('math'=>'<', 'value'=>$seq), 
	        											   'check_dis'=> array('math'=>'=', 'value'=>'1'),
	        											   'danhmuc_id'=> array('math'=>'=', 'value'=>$this->danhmucid),
        												   'iddm'=> array('math'=>'=', 'value'=>$this->iddm),
        												   'iddmc'=> array('math'=>'=', 'value'=>$this->subiddm)), true);
	        	} else {
	        		$seqMax = $ojbSanPham->getMinMax(array('seq'=>array('math'=>'<', 'value'=>$seq), 'check_dis'=> array('math'=>'=', 'value'=>'1')), true);
	        	}
	        	$idMax = $ojbSanPham->getIdBySeq($seqMax);
	        	
	        	$ojbSanPham->setAttribute('seq', $seqMax);
	        	$ojbSanPham->update(array('id' => $id));
	        	$ojbSanPham->emptyAttribute();
	        	$ojbSanPham->setAttribute('seq', $seq);
	        	$ojbSanPham->update(array('id' => $idMax));
        	}
        }
        
        if ($this->iddm != '' && $this->subiddm != '') {
        	$this->maxseq = $ojbSanPham->getMinMax(array('check_dis'=> array('math'=>'=', 'value'=>'1'),
        												 'danhmuc_id'=> array('math'=>'=', 'value'=>$this->danhmucid),
        												 'iddm'=> array('math'=>'=', 'value'=>$this->iddm),
        												 'iddmc'=> array('math'=>'=', 'value'=>$this->subiddm)), true);
        	$this->minseq = $ojbSanPham->getMinMax(array('check_dis'=> array('math'=>'=', 'value'=>'1'),
        												 'danhmuc_id'=> array('math'=>'=', 'value'=>$this->danhmucid),
        												 'iddm'=> array('math'=>'=', 'value'=>$this->iddm),
        												 'iddmc'=> array('math'=>'=', 'value'=>$this->subiddm)));
        } else {
        	$this->maxseq = $ojbSanPham->getMinMax(array('check_dis'=> array('math'=>'=', 'value'=>'1')), true);
        	$this->minseq = $ojbSanPham->getMinMax(array('check_dis'=> array('math'=>'=', 'value'=>'1')));
        }
        
        $field = "d.name as nameD, c.name as nameC, s.id, s.seq, s.danhmuc_id, s.iddm, s.iddmc, s.code, s.name as nameS, s.gia1, s.gia2, s.check_dis, s.check_km";
        if ($this->iddm != '' && $this->subiddm != '') {
        	$condition = array('s.check_dis' => array('value'=>'1', 'math' => '='),
        					   's.danhmuc_id'=> array('math'=>'=', 'value'=>$this->danhmucid),
        					   's.iddm'=> array('math'=>'=', 'value'=>$this->iddm),
        					   's.iddmc'=> array('math'=>'=', 'value'=>$this->subiddm));
        } else {
        	$condition = array('s.check_dis' => array('value'=>'1', 'math' => '='));
        }
        $this->dataDisplay = $ojbSanPham->sanPhamHome($condition, $field, "s.seq");
        
        // Process view
        $objView->assignobj($this);
        $objView->display();
    }
}
?>
